Imports Clo = Enhancements.Core.Logging
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Represents a format applicable to a separator part in a System.DateTime instance.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class DateTimeFormatSeparator
        Inherits DateTimeFormat

        ''' <summary>Initializes a DateTimeFormatSeparator instance with default values.</summary>
        Public Sub New()
        End Sub

        ''' <summary>Initializes a DateTimeFormatSeparator instance with defined values.</summary>
        ''' <param name="_culture">Culture used to fetch the separator.</param>
        ''' <param name="_value">Type of separator to use.</param>
        ''' <exception cref="IsNullException">_culture is null.</exception>
        Public Sub New(ByVal _culture As Glo.CultureInfo, ByVal _value As DateTimeFormatSeparatorValue)
            Culture = _culture
            Value = _value
        End Sub

        Private __Culture As Glo.CultureInfo = Glo.CultureInfo.CurrentCulture
        ''' <summary>Gets and sets the culture used to fetch the separator. The default value is 'System.Globalization.CultureInfo.CurrentCulture'.</summary>
        ''' <exception cref="IsNullException">_value is null.</exception>
        Public Property Culture() As Glo.CultureInfo
            Get
                Return __Culture
            End Get
            Set(ByVal _value As Glo.CultureInfo)
                _value.IsNullNot("Culture._value", True)

                __Culture = _value
            End Set
        End Property

        Private __Value As DateTimeFormatSeparatorValue = DateTimeFormatSeparatorValue.DateParts
        ''' <summary>Gets and sets the separator to use. The default value is 'DateTimeFormatSeparatorValues.DateParts'.</summary>
        Public Property Value() As DateTimeFormatSeparatorValue
            Get
                Return __Value
            End Get
            Set(ByVal _value As DateTimeFormatSeparatorValue)
                __Value = _value
            End Set
        End Property

        ''' <summary>Builds a format expression according to the instance values.</summary>
        ''' <returns> </returns>
        Public Overrides Function ExpressionBuild() As System.String
            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)
            _returnValueStringBuilder.Append(___ExpressionPrefix)
            _returnValueStringBuilder.Append("separator")
            _returnValueStringBuilder.Append(___ExpressionSeparatorTitleProperties)
            _returnValueStringBuilder.Append("culture")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(Culture.Name)
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append("value")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(Value.ToString().ToLowerInvariant())
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append(___ExpressionSuffix)

            Return _returnValueStringBuilder.ToString()
        End Function

        ''' <summary>Formats a supplied value according to the instance values.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <returns> </returns>
        Public Overrides Function Format(ByVal _value As Sys.DateTime) As System.String
            Dim _returnValue As System.String = Sys.String.Empty
            Select Case Value
                Case DateTimeFormatSeparatorValue.DateParts
                    _returnValue = Culture.DateTimeFormat.DateSeparator
                Case DateTimeFormatSeparatorValue.TimeParts
                    _returnValue = Culture.DateTimeFormat.TimeSeparator
            End Select

            Return _returnValue
        End Function

        Friend Const ___RegularExpression As System.String = "^{separator:culture=(.*);value=(dateparts|timeparts);}$"

        ''' <summary>Tries to parse a format expression, returning an according instance in the _result parameter and True in the return value, when valid. When not, null is put in _result and False is returned.</summary>
        ''' <param name="_expression">Expression to inspect.</param>
        ''' <param name="_result">Instance returned when a valid format expression is used.</param>
        ''' <returns>Whether the operation proceeded normally.</returns>
        Public Shared Function TryParse(ByVal _expression As System.String, ByRef _result As DateTimeFormatSeparator) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Try
                _expression.IsInRegularExpression("_expression", ___RegularExpression, True)
                Dim _culture As Glo.CultureInfo = Glo.CultureInfo.GetCultureInfo(Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(1).Value)
                Dim _value As DateTimeFormatSeparatorValue = CType(Sys.Enum.Parse(GetType(DateTimeFormatSeparatorValue), Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(2).Value, True), DateTimeFormatSeparatorValue)
                _result = New DateTimeFormatSeparator(_culture, _value)
                _returnValue = True
            Catch
                _result = Nothing
            End Try

            Return _returnValue
        End Function

    End Class
End Namespace
